import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
// 前台路由
const commonRoutes = [
    {
        path: '/return_url',
        name: 'returnUrl',
        meta: { title: '支付成功' },
        component: () => import('../components/ReturnUrl.vue'),
    },
    {
        path: '/login',
        name: 'login',
        meta: { title: '登录' },
        component: () => import('@/components/Login.vue'),
    },
    {
        path: '/register',
        name: 'register',
        meta: { title: '注册' },
        component: () => import('@/components/Register.vue'),
    },
    {
        path: '/forgetPwd',
        name: 'forgetPwd',
        meta: { title: '忘记密码' },
        component: () => import('@/components/ForgetPwd.vue'),
    },
    {
        path: '/addLoginAuth',
        name: 'addLoginAuth',
        meta: { title: '登录授权', requireAuth: true },
        component: () => import('@/components/AddLoginAuth.vue'),
    },
    {
        path: '/404',
        name: '404',
        meta: { title: '404' },
        component: () => import('@/components/404.vue'),
    },
    {
        path: '/',
        name: 'home',
        component: () => import('@/components/users/Index.vue'),
        redirect: '/home',
        children: [
            {
                path: '/home',
                name: 'UsersHome',
                meta: { title: '首页' },
                component: () => import('@/components/users/Home.vue'),
            },
            {
                path: '/goodsInfo/:gId',
                name: 'goodsInfo',
                meta: { title: '商品详情' },
                component: () => import('@/components/users/goods/GoodsInfo.vue'),
            },
            {
                path: '/addGoods',
                name: 'addGoods',
                meta: { title: '发布商品信息', requireAuth: true, requireRole: 'business' },
                component: () => import('@/components/users/goods/AddGoods.vue'),
            },
            {
                path: '/addGoodImgs/:gId',
                name: 'addGoodImgs',
                meta: { title: '发布商品图片', requireAuth: true, requireRole: 'business' },
                component: () => import('@/components/users/goods/AddGoodImgs.vue'),
            },
            {
                path: '/updateGoods/:gId',
                name: 'updateGoods',
                meta: { title: '修改商品信息', requireAuth: true, requireRole: 'business' },
                component: () => import('@/components/users/goods/UpdateGoods.vue'),
            },
            {
                path: '/updateGoodImgs/:gId',
                name: 'updateGoodImgs',
                meta: { title: '修改商品图片', requireAuth: true, requireRole: 'business' },
                component: () => import('@/components/users/goods/UpdateGoodImgs.vue'),
            },
            {
                path: '/goodsList',
                name: 'goodsList',
                meta: { title: '商城' },
                component: () => import('@/components/users/goods/GoodsList.vue'),
            },
            {
                path: '/wantGoodsInfo/:wgId',
                name: 'wantGoodsInfo',
                meta: { title: '求购商品详情' },
                component: () => import('@/components/users/wantGoods/WantGoodsInfo.vue'),
            },
            {
                path: '/addWantGoods',
                name: 'addWantGoods',
                meta: { title: '发布求购商品信息', requireAuth: true, requireRole: 'business' },
                component: () => import('@/components/users/wantGoods/AddWantGoods.vue'),
            },
            {
                path: '/addWantGoodImgs/:wgId',
                name: 'addWantGoodImgs',
                meta: { title: '发布求购商品图片', requireAuth: true, requireRole: 'business' },
                component: () => import('@/components/users/wantGoods/AddWantGoodImgs.vue'),
            },
            {
                path: '/updateWantGoods/:wgId',
                name: 'updateWantGoods',
                meta: { title: '修改求购商品信息', requireAuth: true, requireRole: 'business' },
                component: () => import('@/components/users/wantGoods/UpdateWantGoods.vue'),
            },
            {
                path: '/updateWantGoodImgs/:wgId',
                name: 'updateWantGoodImgs',
                meta: { title: '修改求购商品图片', requireAuth: true, requireRole: 'business' },
                component: () => import('@/components/users/wantGoods/UpdateWantGoodImgs.vue'),
            },
            {
                path: '/wantGoodsList',
                name: 'wantGoodsList',
                meta: { title: '求购商城' },
                component: () => import('@/components/users/wantGoods/WantGoodsList.vue'),
            },
            {
                path: '/addUserAuth',
                name: 'addUserAuth',
                meta: { title: '实名认证', requireAuth: true },
                component: () => import('@/components/users/userAuth/AddUserAuth.vue'),
            },
            {
                path: '/updateAlipay',
                name: 'updateAlipay',
                meta: { title: '修改支付宝号', requireAuth: true, requireRole: 'business' },
                component: () => import('@/components/users/userAuth/UpdateAlipay.vue'),
            },
            {
                path: '/addPrivateMsg/:id',
                name: 'addPrivateMsg',
                meta: { title: '发送私信', requireAuth: true },
                component: () => import('@/components/users/privateMsg/AddPrivateMsg.vue'),
            },
            {
                path: '/userInfo/:uId',
                name: 'userInfo',
                meta: { title: '用户主页' },
                component: () => import('@/components/users/userAuth/UserInfo.vue'),
            },
            {
                path: '/shopCar',
                name: 'shopCar',
                meta: { title: '购物车', requireAuth: true },
                component: () => import('@/components/users/shopCar/ShopCar.vue'),
            },
            {
                path: '/confirmOrder',
                name: 'confirmOrder',
                meta: { title: '确认订单', requireAuth: true, requireRole: 'business' },
                component: () => import('@/components/users/orders/ConfirmOrder.vue'),
            },
            {
                path: '/userIndex',
                name: 'userIndex',
                component: () => import('@/components/users/userHome/UserIndex.vue'),
                redirect: '/userHome',
                children: [
                    {
                        path: '/userHome',
                        name: 'userHome',
                        meta: { title: '个人主页', requireAuth: true },
                        component: () => import('@/components/users/userHome/UserHome.vue'),
                    },
                    {
                        path: '/receiveAddress',
                        name: 'receiveAddress',
                        meta: { title: '收货地址', requireAuth: true },
                        component: () => import('@/components/users/userHome/ReceiveAddress.vue'),
                    },
                    {
                        path: '/updatePwd',
                        name: 'updatePwd',
                        meta: { title: '修改密码', requireAuth: true },
                        component: () => import('@/components/users/userHome/UpdatePwd.vue'),
                    },
                    {
                        path: '/myFollow',
                        name: 'myFollow',
                        meta: { title: '我的关注', requireAuth: true },
                        component: () => import('@/components/users/userHome/MyFollow.vue'),
                    },
                    {
                        path: '/myCollection',
                        name: 'myCollection',
                        meta: { title: '我的收藏', requireAuth: true },
                        component: () => import('@/components/users/userHome/MyCollection.vue'),
                    },
                    {
                        path: '/myGoods',
                        name: 'myGoods',
                        meta: { title: '商品管理', requireAuth: true, requireRole: 'business' },
                        component: () => import('@/components/users/userHome/MyGoods.vue'),
                    },
                    {
                        path: '/myWants',
                        name: 'myWants',
                        meta: { title: '求购管理', requireAuth: true, requireRole: 'business' },
                        component: () => import('@/components/users/userHome/MyWants.vue'),
                    },
                    {
                        path: '/myOrders',
                        name: 'myOrders',
                        meta: { title: '订单管理', requireAuth: true, requireRole: 'business' },
                        component: () => import('@/components/users/userHome/MyOrders.vue'),
                    },
                    {
                        path: '/myInfo',
                        name: 'myInfo',
                        meta: { title: '我的消息', requireAuth: true },
                        component: () => import('@/components/users/userHome/MyInfo.vue'),
                    },
                ],
            },
        ],
    },
    {
        path: '/admin',
        redirect: '/admin/home',
    },
]

// 后台路由
export const asyncRoutes = {
    AdminHome: {
        path: '/admin/home',
        name: 'AdminHome',
        meta: { title: '主页' },
        component: () => import('@/components/admin/Home.vue'),
    },
    t1: {
        path: '/admin/t1',
        name: 't1',
        meta: { title: '表格' },
        component: () => import('@/components/admin/T1.vue'),
    },
    password: {
        path: '/admin/password',
        name: 'password',
        meta: { title: '修改密码' },
        component: () => import('@/components/admin/Password.vue'),
    },
    msg: {
        path: '/admin/msg',
        name: 'msg',
        meta: { title: '通知消息' },
        component: () => import('@/components/admin/Msg.vue'),
    },
    userinfo: {
        path: '/admin/userinfo',
        name: 'userinfo',
        meta: { title: '用户信息' },
        component: () => import('@/components/admin/UserInfo.vue'),
    },
}

// 创建路由
const createRouter = () => new Router({
    routes: commonRoutes,
})
const router = createRouter()

// 重置路由
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher
}

export default router